﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using WPHTMLTextBlock.Resources;
using Newtonsoft.Json;
using System.IO;

namespace WPHTMLTextBlock
{
    public partial class MainPage : PhoneApplicationPage
    {
        // Constructor
        public MainPage()
        {
            InitializeComponent();
            // Sample code to localize the ApplicationBar
            //BuildLocalizedApplicationBar();
        }


        private void WebBrowser_ScriptNotify(object sender, NotifyEventArgs e)
        {
            WebBrowser wb = sender as WebBrowser;
            if (wb != null)
            {
                Dictionary<String, String> _json = (Dictionary<String, String>)JsonConvert.DeserializeObject(e.Value, typeof(Dictionary<String, String>));
                if (_json.ContainsKey("offset_Height"))
                {
                    wb.Height = Convert.ToInt32(Math.Abs(Convert.ToDouble(_json["offset_Height"])));
                }
            }
        }

        private void WebBrowser_Loaded(object sender, RoutedEventArgs e)
        {
            WebBrowser wb = sender as WebBrowser;
            if (wb!= null)
            {
                try
                {
                    using (StreamReader sr = new StreamReader("SampleHTML.txt"))
                    {
                        String line = sr.ReadToEnd();
                        wb.NavigateToString(WebBrowserHelper.WrapHtml(line, App.Current.RootVisual.RenderSize.Width));
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show("Could not be read:");
                    MessageBox.Show(ex.Message);
                }
            }
           
        }

        // Sample code for building a localized ApplicationBar
        //private void BuildLocalizedApplicationBar()
        //{
        //    // Set the page's ApplicationBar to a new instance of ApplicationBar.
        //    ApplicationBar = new ApplicationBar();

        //    // Create a new button and set the text value to the localized string from AppResources.
        //    ApplicationBarIconButton appBarButton = new ApplicationBarIconButton(new Uri("/Assets/AppBar/appbar.add.rest.png", UriKind.Relative));
        //    appBarButton.Text = AppResources.AppBarButtonText;
        //    ApplicationBar.Buttons.Add(appBarButton);

        //    // Create a new menu item with the localized string from AppResources.
        //    ApplicationBarMenuItem appBarMenuItem = new ApplicationBarMenuItem(AppResources.AppBarMenuItemText);
        //    ApplicationBar.MenuItems.Add(appBarMenuItem);
        //}
    }
}